package com.qzn.backend.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qzn.backend.dto.DoReportDto;
import com.qzn.backend.dto.DoReportResultDto;
import com.qzn.backend.dto.GetReportDto;
import com.qzn.backend.dto.GetReportResultDto;
import com.qzn.backend.dto.item.GetReportResultItemDto;
import com.qzn.backend.service.ReportService;

/**
 * @author qu.zn
 *
 */
@RequestMapping(value = "report")
@RestController
public class ReportController {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ReportService reportService;

	/**
	 * 报告信息<br>
	 * 包括文本等<br>
	 * type=1、 2、 3
	 * 
	 * @param user
	 * @param type
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "doreporttxt")
	public DoReportResultDto doReportTxt(
			@RequestParam(defaultValue = "") String user,
			@RequestParam(defaultValue = "0") String type,
			@RequestParam(defaultValue = "") String content, String filename,
			String touser, String position) {

		DoReportResultDto result = new DoReportResultDto();

		try {
			// 设置参数
			DoReportDto reportDto = new DoReportDto();
			reportDto.setUser(user);
			reportDto.setType(type);
			reportDto.setContent(content);
			reportDto.setMedia(null);
			reportDto.setFilename(filename);
			reportDto.setTouser(touser);
			reportDto.setPosition(position);

			// report操作
			reportService.doReport(reportDto);

		} catch (Exception e) {
			result.setStatus("-1");
			log.error("do report error" + e);
		}

		return result;
	}

	/**
	 * 报告信息<br>
	 * 包括文本、录音、照片等<br>
	 * type=1、 2、 3
	 * 
	 * @param user
	 * @param type
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "doreport")
	public DoReportResultDto doReport(
			@RequestParam(defaultValue = "") String user,
			@RequestParam(defaultValue = "0") String type,
			@RequestParam(defaultValue = "") String content,
			MultipartFile media, String filename, String touser, String position) {

		DoReportResultDto result = new DoReportResultDto();

		try {
			// 设置参数
			DoReportDto reportDto = new DoReportDto();
			reportDto.setUser(user);
			reportDto.setType(type);
			reportDto.setContent(content);
			reportDto.setMedia(media == null ? null : media.getBytes());
			reportDto.setFilename(filename);
			reportDto.setTouser(touser);
			reportDto.setPosition(position);

			// report操作
			reportService.doReport(reportDto);

		} catch (Exception e) {
			result.setStatus("-1");
			log.error("do report error" + e);
		}

		return result;
	}

	/**
	 * 获取报告<br>
	 * 文本信息直接获取<br>
	 * 图片和语音单独获取/getmedia
	 * 
	 * @param user
	 * @param pageno
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "getreport")
	public GetReportResultDto getReport(String user, int pageno, int size) {

		GetReportResultDto result = new GetReportResultDto();

		try {
			GetReportDto reportDto = new GetReportDto();
			reportDto.setUser(user);
			reportDto.setPageno(pageno);
			reportDto.setSize(size);
			reportDto.setOffset();

			// 获取报告列表
			List<GetReportResultItemDto> items = reportService
					.getReport(reportDto);
			result.setItems(items);

		} catch (Exception e) {
			result.setStatus("-1");
			log.error("get report error" + e);
		}

		return result;

	}

	/**
	 * 获取图片或者语音
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getmedia")
	public ResponseEntity<byte[]> getMedia(String id, String filename) {

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", filename);
		try {

			// 获取媒体流
			Map<String, byte[]> mediaMap = reportService.getMedia(id);

			if (mediaMap == null) {
				return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
			}

			return new ResponseEntity<byte[]>(mediaMap.get("media"), headers,
					HttpStatus.OK);

		} catch (Exception e) {
			log.error("get media error" + e);
		}

		return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);

	}

}
